from random import random
import time
import hmac
import hashlib
import base64
import urllib.parse

import requests, json  #导入依赖库

#定义要发送的数据
#"at": {"atMobiles": "['"+ mobile + "']"
class Ab:
    def __init__(self):
        self.timestamp = str(round(time.time() * 1000))
        self.secret = 'SEC66a153bae344b8ef3d2e6d6a9619cbfcf70e1744c273b42398176bb2d7630a48'
        self.secret_enc =self.secret.encode('utf-8')
        self.string_to_sign = '{}\n{}'.format(self.timestamp, self.secret)
        self.string_to_sign_enc = self.string_to_sign.encode('utf-8')
        self.hmac_code = hmac.new(self.secret_enc, self.string_to_sign_enc,digestmod=hashlib.sha256).digest()
        self.sign = urllib.parse.quote(base64.b64encode(self.hmac_code))
        self.headers = {'Content-Type': 'application/json'}  #定义数据类型
        self.webhook = 'https://oapi.dingtalk.com/robot/send?access_token=5a3e8d5b188ec4c15cadf0f212015bbcdd5514bf5f02fc0241e88f222bef976a&timestamp=' + self.timestamp + "&sign=" + self.sign

    def send_sms(self,msg):

        data = {
            "msgtype": "text",
            "text": {
                "content":msg
            },
            "isAtAll": True
        }
        res = requests.post(self.webhook, data=json.dumps(data), headers=self.headers)  #发送post请求

        print(res.text)

ab = Ab()
ab.send_sms("75654")